package com.example.hzq.zqb.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.example.hzq.zqb.R;
import com.example.hzq.zqb.adapter.CommentAdapter;
import com.example.hzq.zqb.adapter.NetworkImageHolderView;
import com.example.hzq.zqb.bean.CommentBean;
import com.example.hzq.zqb.bean.GrandgoodsBean;
import com.example.hzq.zqb.model.Model;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.w3c.dom.Comment;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/14.
 */

public class GranddetailActivity extends AppCompatActivity {
    private ImageButton back;
    private GrandgoodsBean grandgoodsBean;
    private ConvenientBanner convenientBanner;
    private List<String> networkImages= new ArrayList<String>();
    private ImageLoader imageLoader;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private int page,lastVisibleItemPosition;
    private List<CommentBean>list=null;
    private CommentAdapter commentAdapter;
    private Spinner spinner;
    private String type[]={"iphone6","iphone6 Plus","iphone7","iphone7 Plus","iphone8","iphone8 Plus"};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.granddetail);
        grandgoodsBean=(GrandgoodsBean)getIntent().getSerializableExtra("grandgood");
        page=1;
        init();
        netinit();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void init(){
        convenientBanner=(ConvenientBanner)findViewById(R.id.bd_conven);
        spinner=(Spinner)findViewById(R.id.bd_spinner);
        back=(ImageButton)findViewById(R.id.back1);
        recyclerView=(RecyclerView)findViewById(R.id.bd_recycler);
        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        commentAdapter=new CommentAdapter(GranddetailActivity.this);
        commentAdapter.setList(list);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(GranddetailActivity.this,android.R.layout.simple_spinner_item,type){
            @NonNull
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                return setCentered(super.getView(position, convertView, parent));
            }

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent)
            {
                return setCentered(super.getDropDownView(position, convertView, parent));
            }

            private View setCentered(View view)
            {
                TextView textView = (TextView)view.findViewById(android.R.id.text1);
                textView.setGravity(Gravity.CENTER);
                return view;
            }
        };
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(adapter);
        Model model=new Model();
        Call<List<CommentBean>> call=model.getCommentList(page);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    list=response.body();
                    commentAdapter.setList(list);
                }
                else  Toast.makeText(GranddetailActivity.this,"获取资源失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                Toast.makeText(GranddetailActivity.this,"404.grandactivity", Toast.LENGTH_SHORT).show();
            }
        });
       recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page+=1;
                    Model model=new Model();
                    Call<List<CommentBean>> call=model.getCommentList(page);
                    call.enqueue(new Callback<List<CommentBean>>() {
                        @Override
                        public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                            if(response.isSuccessful()&&response.body().size()!=0)
                            {
                                list.addAll(response.body());
                                commentAdapter.setList(list);
                            }
                            else{
                                Toast.makeText(GranddetailActivity.this,"没有更多了", Toast.LENGTH_SHORT).show();
                            }
                        }
                        @Override
                        public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                            Toast.makeText(GranddetailActivity.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=list.size()-1;
            }
        });
        recyclerView.setAdapter(commentAdapter);
    }
    private void netinit() {
        imageLoader=ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        for(int i=0;i<grandgoodsBean.getImages().size();i++){
            networkImages.add(imageurl+grandgoodsBean.getImages().get(i));
        }
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImages).setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .startTurning(2000);
    }
}
